<template>
  <div style="position:relative;">
    <loading></loading>
    <error></error>
    <div class="g-legend-wrapper">
      <span class="g-legend-title g-font-f5">
        {{currentTime}}
      </span>
      <div class="g-legend-content">
        <span class="g-font-f5 g-legend-item red">
          {{legendText.redText}}
          <span class="number">{{positive}}只</span>
        </span>
        <span class="g-font-f5 g-legend-item green">
          {{legendText.greenText}}
          <span class="number"> {{negative}}只</span>
        </span>
        <span :class="['g-font-f5', 'g-legend-item', 'blue',{padTap: topSelected}]">
          {{legendText.blueText}}
          <span class="number">{{index}}</span>
        </span>
      </div>
    </div>
    <div ref="chartLine" class="chart-line" style="{width:1.875rem;height: 2.43rem;padding: .1rem 0;}">
    </div>
  </div>
</template>

<script>
import echarts from 'echarts/lib/echarts';
// 引入柱状图
import 'echarts/lib/chart/line';
// 引入提示框和标题组件
import 'echarts/lib/component/tooltip';

import { getTradeTimeArr, extend } from '@/common/common'

const tradeDateArr = getTradeTimeArr();

const option = {
  grid: {
    top: '15%',
    bottom: '15%',
    left: '11%',
    right: '13%'
  },
  tooltip: {
    trigger: 'axis'
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: tradeDateArr,
    splitLine: {
      show: true,
      lineStyle: {
        type: 'dotted',
        color: '#bac9d8',
        opacity: 0.7
      },
      interval: 59
    },
    axisTick: {
      show: false
    },      // 隐藏刻度
    axisPointer: {
      show: true,
      type: 'line',
      lineStyle: {
        type: 'solid'
      },
      label: {
        show: false,
        backgroundColor: 'transparent',
        textStyle: {
          color: 'transparent'
        }
      }
    },
    axisLine: {
      show: false
    },
    axisLabel: {
      margin: 12,
      textStyle: {
        color: '#999999',
        fontSize: 10,
        align: 'center'
      },
      interval: 59
    } // x轴 文字样式
  },
  yAxis: [{
    name: '涨跌数(只)',
    nameTextStyle: {
      color: '#555'
    },
    splitLine: {
      lineStyle: {
        type: 'dotted',
        color: '#bac9d8',
        opacity: 0.7
      }
    },
    axisTick: {
      show: false
    },      // 隐藏刻度
    axisLine: {
      show: false
    },
    axisLabel: {
      margin: 20,
      textStyle: {
        color: '#a5a5a5',
        fontSize: 10
      }
    },    // 轴 文字样式
    splitNumber: 2
  }, {
    name: '上证指数',
    nameTextStyle: {
      color: '#555'
    },
    axisTick: {
      show: false
    },    // 隐藏刻度
    splitLine: {
      show: false
    },
    axisLine: {
      show: false
    },
    axisLabel: {
      margin: 12,
      formatter: value => `${value.toFixed(0)}`,
      textStyle: {
        color: '#a5a5a5',
        fontSize: 10
      }
    }   // 轴 文字样式
  }],
  series: [
    {
      name: '上证指数',
      yAxisIndex: 1,
      type: 'line',
      itemStyle: {
        normal: {
          color: '#6c9bd2'
        }
      },
      lineStyle: {
        normal: {
          width: 1,
          color: '#6c9bd2',
          type: 'dotted'
        }
      },
      smooth: true,
      symbol: 'none'
    },
    {
      type: 'line',
      smooth: true,
//      smoothMonotone: 'y',
      symbol: `image://${require('@/assets/images/redSymbol.svg')}`,
      showSymbol: false,
      sampling: 'average',
      lineStyle: {
        normal: {
          width: 1,
          color: '#eb333b'
        }
      }
    },
    {
      type: 'line',
      smooth: true,
      symbol: `image://${require('@/assets/images/greenSymbol.svg')}`,
      showSymbol: false,
      sampling: 'average',
      lineStyle: {
        normal: {
          width: 1,
          color: '#1aae52'
        }
      }
    }
  ]
};

export default {
  name: 'trend-chart',
  data () {
    return {
      currentTime: '-:-',
      index: '',
      positive: '',
      negative: ''
    }
  },
  props: {
    chartData: {
      type: Object
    },
    legendText: {
      type: Object,
      default: function () {
        return {
          redText: '涨停',
          greenText: '跌停',
          blueText: '上证指数'
        }
      }
    },
    topSelected: {
      default: false
    }
  },
  methods: {
    chartSelected(params) {
      this.currentTime = params[0].axisValue.slice(0, 5);
      const params0 = params.find(item => item.seriesIndex == 0)
      const params1 = params.find(item => item.seriesIndex == 1)
      const params2 = params.find(item => item.seriesIndex == 2)

      Object.assign(this.$data, {
        index: params0 ? params0.value : '',
        positive: params1 ? params1.value[1] : '',
        negative: params2 ? params2.value[1] : ''
      });
    },
    legendInit() {
      const time = this.chartData.gain.slice(-1)[0][0];
      const hqIndex = tradeDateArr.indexOf(time);
      this.currentTime = time.slice(0, 5);

      Object.assign(this.$data, {
        index: this.chartData.indexData.index ? this.chartData.indexData.index[hqIndex] || '' : '',
        positive: this.chartData.gain.slice(-1)[0][1],
        negative: this.chartData.slip.slice(-1)[0][1]
      });
    },
    renderChart (val) {
      this.legendInit();
      this.chartLine.clear();
      this.chartLine.setOption(extend(option, {
        yAxis: [{}, {
          name: this.legendText.blueText,
          min: Math.min.apply(null, val.indexData.index),
          max: Math.max.apply(null, val.indexData.index),
          axisLabel: {
            formatter: (v) => {
              return v.toFixed(0)
            }
          }
        }],
        series: [{
          data: val.indexData.index
        }, {
          data: val.gain
        }, {
          data: val.slip
        }],
        tooltip: {
          formatter: this.chartSelected
        }
      }))

      setTimeout(() => {
        this.$store.dispatch('hideLoading')                // 图表渲染成功    loading消失
      }, 0)
    }
  },
  watch: {
    chartData: function (val, oldVal) {
      Object.assign(this.$data, {
        index: '',
        positive: '',
        negative: ''
      });
      this.renderChart(val);
    }
  },
  mounted () {
    this.$store.dispatch('showLoading');
    this.chartLine = echarts.init(this.$refs.chartLine);
    // this.chartLine.setOption(option);
  },
  beforeDestroy () {
    echarts.dispose(this.chartLine);
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
  @import '../../../assets/css/common.less';
  .g-legend-title {
    width: 0.3rem;
    padding-top: 0.01rem;
  }
  .g-legend-wrapper {
    .g-legend-content {
      padding-left: 0.1rem;
      justify-content: flex-start;
      .number {
        min-width: 0.25rem;
        display: inline-block;
      }
      .g-legend-item {
        // width: 30%;
        margin-left: 0.17rem;
        &.blue {
          width: 40%
        }
        &.padTap{
          padding-top: .15rem;
        }
      }
    }
}
</style>
